#include "qimageupdatethread.h"
#include <QTime>
#include <QDir>
#include <QDebug>


QImageUpdateThread::QImageUpdateThread(int startId, int endId, QVector<LockImage *> *lockImages, QObject *parent)
    :QThread(parent),
      mQuit(true),
      mStartId(startId),
      mEndId(endId),
      mLockImageVector(lockImages)

{
    QDir dir("/mnt/share/1920x1080");
    dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    dir.setSorting(QDir::Size | QDir::Reversed);

    QFileInfoList list = dir.entryInfoList();

    for (int i = 0; i < list.size(); ++i) {
        QFileInfo fileInfo = list.at(i);
        mImagePathVector.append(fileInfo.absoluteFilePath());
    }

    qDebug()<<"thread starId"<<startId<<"endId"<<endId;
}

QImageUpdateThread::~QImageUpdateThread()
{
    mQuit = true;
}

int QImageUpdateThread::startUpdateImage()
{
    if(mQuit){
        mQuit = false;
        start();
        return 0;
    }
    return -1;
}

void QImageUpdateThread::run()
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    while (!mQuit) {
        for(int i = mStartId; i <= mEndId; i++){
            QString path = mImagePathVector[qrand()%(mImagePathVector.size() - 1)];
            mLockImageVector->at(i)->loadImage(path);
        }
    }
    mQuit = true;
}

bool QImageUpdateThread::isQuit()
{
    return mQuit;
}

